package com.stenio.fancat.controller.shop;

import com.baomidou.mybatisplus.plugins.Page;
import com.stenio.fancat.controller.Result;
import com.stenio.fancat.domain.shop.FcCategory;
import com.stenio.fancat.domain.shop.FcShopFlea;
import com.stenio.fancat.domain.shop.FcVender;
import com.stenio.fancat.form.shop.ShopFleaForm;
import com.stenio.fancat.service.shop.CategoryService;
import com.stenio.fancat.service.shop.ShopFleaService;
import com.stenio.fancat.service.shop.VenderService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangqiu
 * @date 2018/12/1
 * @desc
 **/
@RestController
@RequestMapping("shopFlea")
public class ShopFleaController {
    @Autowired
    private ShopFleaService shopService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private VenderService venderService;

    @PostMapping("add")
    @ResponseBody
    public Result add(@RequestBody ShopFleaForm shopForm){
        Result result = new Result();
        FcShopFlea shop = shopService.saveWare(shopForm);
        result.setData(shop);
        return result;
    }

    @PostMapping("update")
    @ResponseBody
    public Result update(@RequestBody ShopFleaForm shopForm){
        Result result = new Result();
        result.setData(shopService.updateWare(shopForm));
        return result;
    }

    @GetMapping("get/{id}")
    @ResponseBody
    public Result get(@PathVariable Integer id){
        Result result = new Result();
        FcShopFlea shop = shopService.getById(id);
        result.setData(shopToVo(shop));
        return result;
    }

    @PostMapping("page")
    @ResponseBody
    public Result page(@RequestBody ShopFleaForm shopForm){
        Result result = new Result();
        Page<FcShopFlea> page = shopService.selectByPage(shopForm);
        Map<String,Object> data = new HashMap<>();
        if(page!=null){
            data.put("current",page.getCurrent());
            data.put("pages",page.getPages());
            data.put("size",page.getSize());
            data.put("total",page.getTotal());
            List<ShopFleaVo> records = new ArrayList<>();
            if(CollectionUtils.isNotEmpty(page.getRecords())){
                page.getRecords().stream().forEach(shop->{
                    records.add(shopToVo(shop));
                });
            }
            data.put("records",records);
        }
        result.setData(data);
        return result;
    }

    private ShopFleaVo shopToVo(FcShopFlea shop){
        FcCategory category2 = categoryService.findById(shop.getCategoryId());
        FcCategory category1 = categoryService.findById(category2.getParentId());
        FcVender vender = venderService.findById(shop.getVenderId());
        ShopFleaVo vo = new ShopFleaVo();
        BeanUtils.copyProperties(shop,vo);
        vo.setCat1Name(category1.getName());
        vo.setCat2Name(category2.getName());
        vo.setCatLev1(category1.getId());
        vo.setVenderName(vender.getName());
        return vo;
    }

    @PostMapping("onOff/{id}/{status}")
    @ResponseBody
    public Result onOff(@PathVariable Integer id, @PathVariable Integer status){
        Result result = new Result();
        try {
            result.setData(shopService.onOff(id,status));
        } catch (Exception e) {
            result.setFlag(Result.FLAG.failure);
            result.setMsg("上下架状态不合法");
        }
        return result;
    }
}
